var main=[[ //表头
	{type:'checkbox', fixed: 'left'},
	{field: 'scrap_no', title: '车辆报废单号',  sort: true, fixed: 'left'}
    ,{field: 'number', title: '车牌号',  sort: true,templet:`<div>{{d.number?d.number:"-"}}</div>`}
    ,{field: 'brand', title: '品牌',templet:`<div>{{d.brand?d.brand:"-"}}</div>`} 
    ,{field: 'car_type_name', title: '车型',templet:`<div>{{d.car_type_name?d.car_type_name:"-"}}</div>`}
    ,{field: 'company_name', title: '公司',templet:`<div>{{d.company_name?d.company_name:"-"}}</div>`}
    ,{field:'state2', title:'状态',templet:`<div>{{d.state2?d.state2:"-"}}</div>`}
   	,{ title: '操作',width:250, toolbar:'#operation',fixed: 'right'}
]];
var auto='';
var fi=1;
var page=1;
var data = {};
data.page = page;
data.pagenum = 10;
var where_json = {};

	{if $Think.get.car_audit_id}
		where_json.car_audit_id=['like',"%{$Think.get.car_audit_id}%"];
	{/if}
	{if $Think.get.company_name}
		where_json.company_name=['like',"%{$Think.get.company_name}%"];
	{/if}
	{if $Think.get.number}
		where_json.number=['like',"%{$Think.get.number}%"];
	{/if}
	{if $Think.get.state}
		where_json.state=['in',"{$Think.get.state}"];
	{/if}
	data.where_json = JSON.stringify(where_json)
	ajaxscrap(data)
	function ajaxscrap(data) {
		Load()
	ajax(api.cycle_scrap.list, data, 'post').done(function(res) {
		console.log(res)
		Load(1)
		if(res.code == 1) {
			for(var stateidx = 0; stateidx < res.data.length; stateidx++) {
			if(res.data[stateidx].state == 1) {
					res.data[stateidx].state2 = '待审批'
				} else if(res.data[stateidx].state == 2) {
					res.data[stateidx].state2 = '审批中'
				} else if(res.data[stateidx].state == 3) {
					res.data[stateidx].state2 = '已拒绝'
				} else if(res.data[stateidx].state == 4||res.data[stateidx].state == 5) {
					res.data[stateidx].state2 = '待上传'
				} else if(res.data[stateidx].state == 6) {
					res.data[stateidx].state2 = '完成'
				} 
			}
			table.render({
    				elem: '#buy',
    				cellMinWidth: 80,
    				cols: main,
    				data: res.data,
    				limit: data.pagenum,
    				even: true
    			});
    			if (fi == 1) {
    				laypage.render({
    					elem: 'page',
    					count: res.count,
						layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
						theme: '#1E9FFF',
    					jump: function (obj, first) {
    						if (!first) {
    							page = obj.curr
    							data.page = page
    							pagenum = obj.limit
    							data.pagenum = pagenum
    							ajaxscrap(data)
    						} else {

    						}
    					}
    				});
    				fi = 2
    			}
    		table.on('tool(buy)', function(obj) {
				var data1 = obj.data; //获得当前行数据
				var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
				var tr = obj.tr; //获得当前行 tr 的DOM对象
				if(layEvent === 'detail') {
					detailscrap(data1)
				}else if(layEvent === 'check_scrap1'){
					check_scrap(data1,1)
				}else if(layEvent === 'check_scrap2'){ //审核
					check_scrap(data1,2)
				}else if(layEvent === 'scrap_affirm'){ //确认
					check_scrap(data1,3)
				}else if(layEvent === 'sumbit_scrap1'){ //确认
					sumbit_scrap(data1,1)
				}else if(layEvent === 'sumbit_scrap2'){ //确认
					sumbit_scrap(data1,2)
				}else if(layEvent === 'scrap_delete'){ //确认
					scrap_delete(data1)
				}else if(layEvent === 'detail_scrap'){ //确认
					detail_scrap(data1)
				}else if(layEvent === 'look'){ //删除
					window.location.href='/html/car/scrap_detail?car_scrap_id='+data1.car_scrap_id+'&car_id='+data1.car_id
				}
				
				
			})
    		
			
			
			
		} else {
			Err('请求失败')
		}
	}).fail(

	)
}
$('.add').on('click',function(res){
	propcheck()
})
//申请
	function propcheck(obj, main) {
		var auto = layer.open({
			type: 1,
			title: '车辆报废申请',
			skin: 'layui-layer-rim', //加上边框
			area: ['600px', '350px'], //宽高
			content: `
   			<div class='layui-row' style='padding:10px'>
   			<form class="layui-form">
	   			<div class='layui-col-sm12'>
	   				<div  style='margin-bottom:10px'>
		   				<div class="layui-form-item " style="padding-right:10px">
		   				<div class="layui-input-inline">
		   					<input type="hidden" class="car_id" name="car_id" value="{$item.car_id}" />
							<input type="text" class="layui-input number" value="{$item.car_number}" name="car_number" data-check='require' data-msg="车牌号不能为空" readonly="" />
		   				</div>
		   				<div class='layui-btn-group am-fl layui-btn-group-xs cl-p' >
		   					<button type="button" class='layui-btn  car_detail'>查看车辆详情</button>
		   				</div>
	   					</div>
	   				</div>
	   				<div >
		   				<div class='layui-col-sm12'>
			   				<textarea style='height:150px;width:100%;padding:10px' class='why' placeholder='请填写报废理由'></textarea>
		   				</div>
	   				</div>
	   				
	   			</div>
	   			
	   			<div class='layui-col-sm12' style="text-align:center;margin-top:10px">
   					<button type='button' class='layui-btn apply_ok'>提交申请</button>
   					<button type='button' class='layui-btn apply_no'>取消</button>
	   			</div>
	   		</form>
   			</div>
   		`,
			success: function() {
				$('.number').on('click', function() {
					opsearch("{:url('/html/car/car')}?op=search")
				});
				$('.car_detail').on('click', function() {
					opsearch("{:url('/html/car/car')}?op=search")
//					if($('.CarId').val() == "") {
//						Err('请选择车辆')
//					} else {
//						opsearch("{:url('/html/car/car_detail')}?op=search2&car_id=" + $('.car_id').val());
//						//					choosecar("{:url('/html/car/car_detail')}?car_id="+$('.CarId').val(),'车辆详情')
//					}
				})
				$('.apply_ok').on('click', function() {
					Load()
					ajax(api.cycle_scrap.add, {
						car_id: $('.car_id').val(),
						why: $('.why').val()
					}, 'post').done(function(res) {
						Load(1)
						if(res.code == 1) {
							console.log(res)
							Msg('已提交申请')
							ajaxscrap(data)
							layer.close(auto)
						} else {
							Err('请求失败')
						}
					}).fail(function() {
						Load(1)
						Err('请求失败')
					})

				})
				$('.apply_no').on('click', function() {
					layer.close(auto)
				})
			}
		})
	}
	//删除	
	function scrap_delete(obj) {
		auto = layer.open({
			type: 1,
			title: false,
			skin: 'layui-layer-rim', //加上边框
			area: ['200px', '100px'], //宽高
			content: `
	   			<div class='layui-col-sm12' style='padding:10px'>
	   				<div class='layui-col-sm12' style='text-align:center;margin-bottom:10px'>是否删除该报废单</div>
	   				<div class='layui-col-sm12' style='text-align:center'>
	   					<a class='layui-btn am-radius close_ok'>是</a>
	   					<a class='layui-btn am-radius close_no'>否</a>
	   				</div>
	   			</div>
	   		`,
			success: function() {

				$('.close_ok').on('click', function() {
					Load()
					ajax(api.cycle_scrap.delete,{
	   						car_scrap_id: obj.car_scrap_id
	   					},'post').done((res)=>{
	   						Load(1)
							console.log(res)
							if(res.code == 1) {
								ajaxscrap(data)
								Msg('删除成功')
							}
							layer.close(auto)
	   					}).fail(()=>{
	   						Load(1)
							Err('请求失败')
							layer.close(auto)
	   					})
				});
				$('.close_no').on('click', function() {
					layer.close(auto)
				})
			}
		})
	}

	console.log(JSON.parse($.cookie('user')).role_id.split(','))
	//审批
	function check_scrap(obj, type) {
		let check_data = {
			car_scrap_id: obj.car_scrap_id,
		}
		let main = '';
		if(type == 3) {
			main = '是否确认该车已完成报废'
		} else {
			main = '是否同意该车辆进行报废'
		}
		auto = layer.open({
			type: 1,
			title: false,
			skin: 'layui-layer-rim', //加上边框
			area: ['300px', '150px'], //宽高
			content: `
				<div class='layui-col-sm12' style='padding:10px'>
					<div class='layui-col-sm12' style='text-align:center;margin-bottom:10px'>${obj.number} <button type="button" class='layui-btn layui-btn-sm  car_detail'>查看车辆详情</button></div>
					<div class='layui-col-sm12' style='text-align:center;margin-bottom:10px'>${main}</div>
					<div class='layui-col-sm12' style='text-align:center'>
						<a class='layui-btn layui-btn-sm layui-btn-primary am-radius close_ok'>同意</a>
						<a class='layui-btn layui-btn-sm layui-btn-primary am-radius close_no'>拒绝</a>
					</div>
				</div>
			`,
			success: function() {
				$('.car_detail').on('click', function() {
					window.open("{:url('html/car/car_detail')}?_blank=1&car_id=" + obj.car_id);
				})
				$('.close_ok').on('click', function() {
					if(type == 1) {
						check_data.bureau_of_finance_result = 1
					} else if(type == 2) {
						check_data.provincial_administration_result = 1
					} else if(type == 3) {
						check_data.provincial_administration_again_result = 1
					}
					Load()
					ajax(api.cycle_scrap.check, check_data, 'post').done((res) => {
						console.log(res)
						Load(1)
						if(res.code == 1) {
							ajaxscrap(data)
							layer.close(auto)
						} else {
							if(res.msg) {
								Err(res.msg)
							}

						}
					}).fail(() => {
						Load(1);
						Err('请求失败')
					})
				})
				$('.close_no').on('click', function() {
					if(type == 1) {
						check_data.bureau_of_finance_result = 9
					} else if(type == 2) {
						check_data.provincial_administration_result = 9
					} else if(type == 3) {
						check_data.provincial_administration_again_result = 9
					}
					Load()
					ajax(api.cycle_scrap.check, check_data, 'post').done((res) => {
						console.log(res)
						Load(1)
						if(res.code == 1) {
							ajaxscrap(data)
							layer.close(auto)
						} else {
							Err(res.msg)
						}
					}).fail(() => {
						Load(1);
						Err('请求失败')
					})
				})
			}

		})
	}

	//上传相关文件
	function sumbit_scrap(obj, type) {
		let check_data = {
			car_scrap_id: obj.car_scrap_id,
		}
		let file = {}
		if(type == 1) {
			file.name = '上传报废图片';
			file.key = 'scrap_image';
		} else {
			file.name = '上传资产处理图片';
			file.key = 'bureau_of_finance_file';
		}
		auto = layer.open({
			type: 1,
			title: false,
			skin: 'layui-layer-rim', //加上边框
			area: ['300px', '225px'], //宽高
			content: `
				<div class='layui-col-sm12' style='padding:10px'>
					<div style='text-align:center'>
						<div class="layui-upload">
							<button type="button" class="layui-btn layui-btn-primary layui-btn-sm" id="${file.key}-btn">${file.name}</button>
							<div class="layui-upload-list">
								<img class="layui-upload-img ${file.key}" data-key=' ${file.key}' id="${file.key}">
								<p id="demoText"></p>
							</div>
						</div>
						<button type="button" class="layui-btn file_ok" id="front_img">确认上传</button>
					</div>
					
				</div>
			`,
			success: function() {
				let filedata={};
				upImage(api.upimage.upimage, '#'+file.key+'-btn', '#'+file.key, filedata, file.key);
				$('.car_detail').on('click', function() {
					if($('.CarId').val() == "") {
						Err('请选择车辆')
					} else {
						window.open("{:url('html/car/car_detail')}?_blank=1&car_id=" + obj.car_id);
						//					choosecar("{:url('/html/car/car_detail')}?car_id="+$('.CarId').val(),'车辆详情')
					}
				})
				$('.file_ok').on('click', function() {
					if(type == 1) {
						check_data.scrap_document = filedata.scrap_image
					} else if(type == 2) {
						check_data.disposition_of_assets = filedata.bureau_of_finance_file
					}
					Load()
					ajax(api.cycle_scrap.check, check_data, 'post').done((res) => {
						console.log(res)
						Load(1)
						if(res.code == 1) {
							ajaxscrap(data)
							layer.close(auto)
						} else {
							if(res.msg) {
								Err(res.msg)
							}

						}
					}).fail(() => {
						Load(1);
						Err('请求失败')
					})
				})
			}

		})
	}
//})
	function detail_scrap(obj){
		console.log(obj)
		auto = layer.open({
			type: 1,
			title: false,
			skin: 'layui-layer-rim', //加上边框
			area: ['400px', '500px'], //宽高
			content: `
				<div class='layui-row'>
					<div class='layui-col-sm12'>
						<div class=' m-label'>车辆报废单号:</div>
						<div class='m-value'>${obj.scrap_no}</div>
					</div>
					<div class='layui-col-sm12'>
						<div class=' m-label'>车牌号:</div>
						<div class='m-value'>${obj.number}</div>
					</div>
					<div class='layui-col-sm12'>
						<div class=' m-label'>报废文件上传时间:</div>
						<div class='m-value'>${obj.number}</div>
					</div>
					<div class='layui-col-sm12'>
						<div class=' m-label'>报废文件:</div>
						<div class='m-value'>
							<img src='${obj.scrap_image}' style='height:100px;width:100px'>
						</div>
					</div>
					<div class='layui-col-sm12'>
						<div class=' m-label'>资产批复上传时间:</div>
						<div class='m-value'>${obj.number}</div>
					</div>
					<div class='layui-col-sm12'>
						<div class=' m-label'>资产批复文件:</div>
						<div class='m-value'>
							<img src='${obj.number}' style='height:100px;width:100px'>
						</div>
					</div>
					
				</div>
			`

		})
	}
